<?php
namespace Admin\Controller;
use Think\Controller;
class PageController extends AdminController {
    public function index(){
        $pMO = M('page');  
        $tCount = $pMO->count();
        $tPage = new \Think\Page($tCount,25);
        $tShow = $tPage->show();
        $tData = $pMO->order('id desc')->limit($tPage->firstRow.','.$tPage->listRows)->select();
        $this->assign('tPage',$tShow);// 赋值分页输出
        $this->assign('tData' , $tData);
        $this->display();

    }
    public function add(){
        $tTime = time(); 
        if(IS_POST){
            $pTitle = I('title' , '' , 'htmlspecialchars'); 
            $pContent = I('content' , '' , 'htmlspecialchars'); 
            $pDescription = I('description' , '' , 'htmlspecialchars'); 
            if(empty($pTitle)){
                $this->error('名称不能为空');
            }
            if(empty($pContent)){
                $this->error('内容不能为空');
            }
            $tData = array(
                'title' => $pTitle,
                'content' => $pContent,
                'description' => $pDescription,
                'created' => $tTime,
            );
            $tMO = M('page');
            if(!$tMO->add($tData)){
                $this->error('提交失败'); 
            }
            $this->success('新增成功', 'index');
            exit;
        }
        $this->display();
    }
    public function edit(){
        $tTime = time(); 

        $pId = I('id' , '' , 'intval'); 
        $tMO = M('page');
        if(empty($pId)){
            $this->error('ID不能为空');
        }

        if(IS_POST){
            $pTitle = I('title' , '' , 'htmlspecialchars'); 
            $pContent = I('content' , '' , 'htmlspecialchars'); 
            $pDescription = I('description' , '' , 'htmlspecialchars'); 
            if(empty($pTitle)){
                $this->error('名称不能为空');
            }
            if(empty($pContent)){
                $this->error('内容不能为空');
            }
            $tData = array(
                'title' => $pTitle,
                'content' => $pContent,
                'description' => $pDescription,
                'updated' => $tTime,
            );
            if(!$tMO->where('id='.$pId)->save($tData)){
                $this->error('提交失败'); 
            }
            $this->success('修改成功', 'index');
            exit;
        }
        $tRow = $tMO->where('id='.$pId)->find();
        $this->assign('tRow' , $tRow);
        $this->display();
    }

}
